This article explains how to install NGINX Open Source.
NGINX Open Source is available in two versions:
Both NGINX Open Source mainline and stable versions can be installed in two ways:
Compiling NGINX from the sources provides you with more flexibility: you can add particular NGINX modules or 3rd party modules and apply latest security patches.
Prior to compiling NGINX from the sources, it is necessary to install its dependencies:
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
$ tar -zxf pcre-8.39.tar.gz
$ cd pcre-8.39
$ ./configure
$ make
$ sudo make install
$ wget http://zlib.net/zlib-1.2.8.tar.gz
$ tar -zxf zlib-1.2.8.tar.gz
$ cd zlib-1.2.8
$ ./configure
$ make
$ sudo make install
$ wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
$ tar -zxf openssl-1.0.2f.tar.gz
$ cd openssl-1.0.2f
$ ./configure darwin64-x86_64-cc --prefix=/usr
$ make
$ sudo make install
NGINX provides source files for both stable and mainline versions. The source files can be downloaded from the NGINX Open Source download page at http://www.nginx.org/en/download.html.
To download and unpack source files for the latest mainline version, type-in the commands:
$ wget http://nginx.org/download/nginx-1.11.7.tar.gz
$ tar zxf nginx-1.11.7.tar.gz
$ cd nginx-1.11.7
To download and unpack source files for the latest stable version, type-in the commands:
$ wget http://nginx.org/download/nginx-1.10.2.tar.gz
$ tar zxf nginx-1.10.2.tar.gz
$ cd nginx-1.10.2
Configure options are specified with the ./configure
script that sets up various NGINX parameters including paths to source and configuration files, compiler options, connection processing methods and the list of modules. The script ends up with creation of the Makefile
required to compile the code and install NGINX.
Example of the configure script:
$ ./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.39
--with-zlib=../zlib-1.2.8
--with-http_ssl_module
--with-stream
--with-mail=dynamic
--add-module=/usr/build/nginx-rtmp-module
--add-dynamic-module=/usr/build/3party_module
The configure script allows you to set paths to NIGNX binary and configuration files, and to dependent libraries such as PCRE or SSL in order to link them statically to NGINX binary.
--prefix=path
./configure
script (excluding paths to libraries) and for the path to the nginx.conf configuration file./usr/local/nginx
.
--sbin-path=path
prefix/sbin/nginx
.
--conf-path=path
prefix/conf/nginx.conf
.-c
option in command-line.
--pid-path=path
nginx.pid
file that stores the process ID of the main process. After installation, the path to the file name can always be changed in the nginx.conf
configuration file using the pid
directive.prefix/logs/nginx.pid
.
--error-log-path=path
nginx.conf
configuration file using the error_log
directive.prefix/logs/error.log
.
--http-log-path=path
nginx.conf
configuration file using the access_log
directive.prefix/logs/access.log
.
--user=name
nginx.conf
configuration file using the user
directive.nobody
.
--group=name
nginx.conf
configuration file using the user
directive.--with-pcre=path
location
directive and for the ngx_http_rewrite_module module.
--with-pcre-jit
pcre_jit
directive).
--with-zlib=path
In the configure script you can also specify compiler-related options.
--with-cc-opt=parameters
--with-cc-opt="-I /usr/local/include"
must be specified.--with-cc-opt="-D FD_SETSIZE=2048"
.
--with-ld-opt=parameters
--with-ld-opt="-L /usr/local/lib"
must be specified.
In the configure script you can redefine an event-based polling method. See Connection Processing Methods for details.
--with-select_module
, --without-select_module
--with-poll_module
, --without-poll_module
NGINX consists of modules. The set of modules as well as other build options are configured with the ./configure
script.
Some modules are built by default – they do not require to be specified in the configure script. However, any default module can be excluded from the NGINX binary with the --without-
configure option in the configure script.
Modules not included by default as well as third-party modules must be explicitly specified in the configure script together with other build options. Such modules can be linked to NGINX binary either statically and will be loaded each time NGINX starts; or dynamically and will be loaded only if they are specified in the NGINX configuration file.
If you do not need a module built by default, you can disable it by including it to the configure script with the --without-
prefix :
$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-stream --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.8 --without-http_empty_gif_module
Module Name | Description |
http_charset_module | Adds the specified charset to the “Content-Type” response header field, can convert data from one charset to another. |
http_gzip_module | Compresses responses using the gzip method, helping to reduce the size of transmitted data by half or more. |
http_ssi_module | Processes SSI (Server Side Includes) commands in responses passing through it. |
http_userid_module | Sets cookies suitable for client identification. |
http_access_module | Limits access to certain client addresses. |
http_auth_basic_module | Limits access to resources by validating the user name and password using the HTTP Basic Authentication protocol. |
http_autoindex_module | Processes requests ending with the slash character (‘/’) and produces a directory listing. |
http_geo_module | Creates variables with values depending on the client IP address. |
http_map_module | Creates variables whose values depend on values of other variables. |
http_split_clients_module | Creates variables suitable for A/B testing, also known as split testing. |
http_referer_module | Blocks access to a site for requests with invalid values in the Referer header field. |
http_rewrite_module | Changes the request URI using regular expressions and return redirects; conditionally selects configurations. Requires the PCRE library. |
http_proxy_module | Passes requests to another server. |
http_fastcgi_module | Passes requests to a FastCGI server |
http_uwsgi_module | Passes requests to a uwsgi server. |
http_scgi_module | Passes requests to an SCGI server. |
http_memcached_module | Obtains responses from a memcached server. |
http_limit_conn_module | Limits the number of connections per the defined key, in particular, the number of connections from a single IP address. |
http_limit_req_module | Limits the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. |
http_empty_gif_module | Emits single-pixel transparent GIF. |
http_browser_module | Creates variables whose values depend on the value of the “User-Agent” request header field. |
http_upstream_hash_module | Enables the hash load balancing method. |
http_upstream_ip_hash_module | Enables the IP hash load balancing method. |
http_upstream_least_conn_module | Enables the least_conn load balancing method. |
http_upstream_keepalive_module | Enables keepalive connections. |
http_upstream_zone_module | Enables the shared memory zone. |
Some NGINX modules are not built by default. You will need to enable them manually by adding to the ./configure
command. The mail, stream, geoip, image_filter, perl and xslt modules can be compiled as dynamic. See Dynamic Modules for details.
Example of the ./configure
command that includes modules:
$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.8 --with-http_ssl_module --with-stream --with-mail
--with-threads
--with-file-aio
--with-ipv6
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module
or --with-http_xslt_module=dynamic
--with-http_image_filter_module
or --with-http_image_filter_module=dynamic
--with-http_geoip_module
or --with-http_geoip_module=dynamic
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_auth_request_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_degradation_module
--with-http_stub_status_module
--with-http_perl_module
or --with-http_perl_module=dynamic
--with-mail
or --with-mail=dynamic
--with-mail_ssl_module
--with-stream
or --with-stream=dynamic
--with-stream_ssl_module
--with-google_perftools_module
--with-cpp_test_module
--with-debug
You can extend NGINX functionality by compiling NGINX Open Source with your own module or a third-party module. Some third-party modules are listed in the https://nginx.com/resources/wiki/modules/ page. Use third-party modules at your own risk as their stability is not guaranteed.
Most modules which are built into NGINX Open Source are statically linked: they are built into NGINX Open Source at compile time and are linked to the NGINX binary statically. These modules can be disabled only after NGINX recompilation.
To compile NGINX Open Source with a statically linked third-party module, in the configure script specify the --add-module=
option and type-in the path to the module:
$ ./configure ... --add-module=/usr/build/nginx-rtmp-module
NGINX modules can also be compiled as a shared object (*.so file) and then dynamically loaded into NGINX Open Source at runtime. This provides more flexibility, as the module can be loaded or unloaded at any time with the load_module
directive specified in the NGINX configuration file. Note that such a module must support dynamic linking.
To compile NGINX Open Source with a dynamically loaded third-party module, in the configure script specify the --add-dynamic-module=
configure option and provide the path to the module:
$ ./configure ... --add-dynamic-module=/path/to/module
The resulting *.so files with dynamic modules can be found after compilation in the prefix/modules/
directory, where the prefix is a directory that keep server files, for example, /usr/local/nginx/modules
. To load a dynamic module, use the load_module
directive after NGINX installation.
See Introducing Dynamic Modules in NGINX 1.9.11 and Extending NGINX for more information.
$ make
$ sudo make install
$ sudo nginx
Installing NGINX Open Source from a package is much easier and faster comparing to building NGINX from the sources, though it lacks flexibility. Pre-built packages are available for Red Hat, CentOS, Debian, Ubuntu and SLES operating systems.
See Configure Arguments on nginx.org for the list of modules included in each pre-built package.
NGINX provides packages for Red Hat/CentOS 5, 5.x, 6, 6.x, 7 and 7.x. The package can be installed from:
$ sudo yum install epel-release
$ sudo yum update
$ sudo nginx -v
nginx version: nginx/1.6.3
$ sudo vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
where:
For example, to get the latest mainline package for CentOS 7.0, insert:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
:wq
$ sudo yum update
$ sudo nginx
$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.11.7
NGINX provides packages for Debian 7.x (“wheezy”) and 8.x (“jessie”). The package can be installed from:
$ sudo apt-get update
$ sudo apt-get install nginx
$ sudo nginx -v
nginx version: nginx/1.6.2
$ sudo wget http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
$ sudo vi /etc/apt/sources.list
deb http://nginx.org/packages/mainline/debian/ codename nginx
deb-src http://nginx.org/packages/mainline/debian/ codename nginx
where:
Version | Codename | Supported Platforms |
7.x | wheezy | x86_64, i386 |
8.x | jessie | x86_64, i386 |
For example, to get the latest mainline package for Debian 8.2 (“jessie”), insert:
deb http://nginx.org/packages/mainline/debian/ jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx
:wq
$ sudo apt-get remove nginx-common
$ sudo apt-get update
$ sudo apt-get install nginx
$ sudo nginx
$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.11.7
NGINX provides packages for Ubuntu 12.04 (“precise”), 14.04 (“trusty”), 15.10 (“wily”) and 16.04 (“xenial”). The package can be installed from:
$ sudo apt-get update
$ sudo apt-get install nginx
nginx version: nginx/1.4.6 (Ubuntu)
$ sudo wget http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
$ sudo vi /etc/apt/sources.list
deb http://nginx.org/packages/mainline/ubuntu/ codename nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx
where:
Version | Codename | Supported Platforms |
12.04 | precise | x86_64, i386 |
14.04 | trusty | x86_64, i386, aarch64/arm64 |
15.10 | wily | x86_64, i386 |
16.04 | xenial | x86_64, i386 |
For example, to get the latest mainline package for Ubuntu 14.04 (“trusty”), insert:
deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx
:wq
$ sudo apt-get remove nginx-common
$ sudo apt-get update
$ sudo apt-get install nginx
$ sudo nginx
$ curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.11.7
NGINX provides packages for SUSE Linux Enterprise Server 12.
To download and install the latest stable version, run the commands:
$ zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx
$ zypper install nginx
To download and install the latest mainline version, run the commands:
$ zypper addrepo -G -t yum -c 'http://nginx.org/packages/mainline/sles/12' nginx
$ zypper install nginx